import json, os, sys, shutil

Import('env')
Import('jnis')

jythonObj = env.Java(target = 'classes', source = '#src/everest/java',
    JAVACLASSPATH = map(lambda x: "src/everest/" + x, Glob('lib/*.jar', strings = True)) +
    ['${EVEREST_HOME}/lib/everest-0.1-rc2.jar']) #TODO: auto search the jar
jythonService = env.Jar(target = 'JythonService.jar', source = jythonObj)

env.MyInstall('${EVEREST_HOME}/lib/external', [jythonService, jnis, Glob('lib/*.jar')])
env.MyInstall('${EVEREST_HOME}/lib/external/MaximaService', Glob('MaximaService/*.*'))

def updateEverestConfig(env):
    with open(str(File('#src/everest/maxima-service.json').srcnode()), 'rb') as f:
        maximaJson = json.load(f)
    maximaJson['implementation']['config']['maximaPath'] = env['MAXIMA_APP']
    with open(env.subst('${EVEREST_HOME}/services/maxima/service.conf'), 'wb') as f:
        json.dump(maximaJson, f, indent = 1)
        
def addMaximaConfig(target, source, env):
    try:
        os.mkdir(env.subst('${EVEREST_HOME}/services/maxima'))
    except:
        pass
    
    updateEverestConfig(env)
    print "Service installed successfully"

if 'DO_PACKAGE' in env:
    env.AlwaysBuild(env.Alias('install-everest-service', ['${EVEREST_HOME}'], addMaximaConfig))

